package info.thereisonlywe.onenesspathfinder.ui;

import info.thereisonlywe.core.project.ProjectConstants;
import info.thereisonlywe.core.project.ProjectLocalization;
import info.thereisonlywe.core.project.ProjectSettings;
import info.thereisonlywe.core.toolkit.GUIToolkit;
import info.thereisonlywe.core.toolkit.ThreadToolkit;
import info.thereisonlywe.core.ui.swing.extensions.listcellrenderer.TooltipCellRenderer;
import info.thereisonlywe.onenesspathfinder.controllers.MasterController;
import info.thereisonlywe.onenesspathfinder.launcher.Main;
import info.thereisonlywe.onenesspathfinder.managers.ResourceManager;
import info.thereisonlywe.quran.Quran;
import info.thereisonlywe.quran.text.QuranicTextList;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.KeyboardFocusManager;
import java.awt.SystemTray;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JRootPane;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class MainFrame {

	protected static JFrame frame;

	protected static NavigationBar view_Navigation;

	protected static QuranBrowserView view_Browser;

	protected static WindowsView view_Windows;

	/**
	 * @wbp.parser.entryPoint
	 */
	public static void init() throws InterruptedException
	{
		if (frame != null) // prevent accidental re-init
			return;

		frame = new JFrame();
		frame.setLayout(new BorderLayout());
		frame.setMinimumSize(new Dimension(800, 600));
		frame.setLocation(GUIToolkit.getCenteredWindowPosition(800, 600));
		frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		frame.setTitle("Oneness Pathfinder " + ProjectSettings.getVersion());
		frame.setIconImages(ResourceManager.getIconImages());

		view_Windows = new WindowsView();
		view_Navigation = new NavigationBar();
		view_Browser = new QuranBrowserView();

		frame.add(view_Navigation, BorderLayout.NORTH);
		frame.add(view_Browser, BorderLayout.CENTER);
		frame.add(view_Windows, BorderLayout.WEST);

		initListeners();
		SwingUtilities.updateComponentTreeUI(frame);
		frame.pack();
		Tray.init();
		frame.setVisible(true);
		frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
		while (!Main.ready)
		{
			ThreadToolkit.sleep(50);
		}
		if (QuranicTextList.getNumberOfShownTexts() == 0)
		{
			TextConfigurationDialog.showDialog();
		}
		UIDelegate.seekToVerse(Quran.getVerse(
				Main.runtimeSettings.getStringAsInt("section"),
				Main.runtimeSettings.getStringAsInt("verse")));
		Runnable go = new Runnable()
		{

			@Override
			public void run()
			{
				UIDelegate.refreshVerse();
				NavigationBar.sectionDetails = Quran.getSectionDetails();
				SwingUtilities.invokeLater(new Runnable()
				{
					@Override
					public void run()
					{
						TooltipCellRenderer renderer = new TooltipCellRenderer();
						renderer.setTooltips(NavigationBar.sectionDetails);
						NavigationBar.comboBox_Section.setRenderer(renderer);
					}
				});
			}
		};
		new Thread(go).start();
	}

	private static void initListeners()
	{
		KeyboardFocusManager manager = KeyboardFocusManager
				.getCurrentKeyboardFocusManager();
		manager.addKeyEventDispatcher(new KeyDispatcher());
		JRootPane rootPane = frame.getRootPane();
		InputMap iMap = rootPane
				.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
		ActionMap aMap = rootPane.getActionMap();
		iMap.clear();
		aMap.clear();

		frame.addWindowListener(new WindowAdapter()
		{
			@Override
			public void windowClosing(WindowEvent e)
			{
				UIDelegate.setOpenDialog(true);
				int n = JOptionPane.showConfirmDialog(null, ProjectLocalization
						.getString(ProjectConstants.CORE_PROJECT, "Quit"),
						"Oneness Pathfinder", JOptionPane.YES_NO_OPTION);
				UIDelegate.setOpenDialog(false);
				if (n == JOptionPane.YES_OPTION) MasterController.exit(0);
			}

			@Override
			public void windowIconified(WindowEvent e)
			{
				if (SystemTray.isSupported())
				{
					frame.setVisible(false);
				}
			}

			@Override
			public void windowDeiconified(WindowEvent e)
			{
				if (SystemTray.isSupported())
				{
					UIDelegate.showFrame();
				}
			}

		});
	}

}
